<?php
class XConfigComponent extends CApplicationComponent
{
	public function getConfigWidget($w_name)
	{
		$file_path = Yii::app()->basePath.DIRECTORY_SEPARATOR.'widgets'.DIRECTORY_SEPARATOR.$w_name.DIRECTORY_SEPARATOR."$w_name.xml";
		$xml = simplexml_load_file($file_path);
		$config = array();
		for ($i=0; $i<count($xml->param);$i++){
			$config["{$xml->param[$i]->name}"] = $xml->param[$i]->value;
		}
		return $config;
	}
	/**
	 * 
	 * function write to xml
	 * @param $config array()
	 */
	public function writeConfigWidget($config,$w_name)
	{
		$file_path = Yii::app()->basePath.DIRECTORY_SEPARATOR.'widgets'.DIRECTORY_SEPARATOR.$w_name.DIRECTORY_SEPARATOR."$w_name.xml";
		$xml = new SimpleXMLElement( '<?xml version="1.0" encoding="utf-8"?><config></config>' );
		foreach ($config as $key => $value){
			$param = $xml->addChild("param");
			$param->addChild('name',$key);
			$param->addChild('value',$value);
		}
		$xml->asXML($file_path);
		return true;
	}
}